$.app.welcome = {
    init: function() {
        $('legend').click(function() {
            var next = $(this).next();
            if(next.is(':hidden')) {
                $(this).find('i').removeClass('icon-double-angle-up');
                $(this).find('i').addClass('icon-double-angle-down');
                next.slideDown(300);
            } else {
                next.slideUp(300);
                $(this).find('i').removeClass('icon-double-angle-down');
                $(this).find('i').addClass('icon-double-angle-up');
            }
        });
        this._initCalendar();
    },
    _initCalendar: function() {
        var calendar = $('#calendar').fullCalendar({
            header: {
                left: 'prev,next today',
                center: 'title',
                right: 'month,agendaWeek,agendaDay'
            },
            events: ctx + '/admin/personal/calendar/load',
            eventDrop: function(event) {
                moveCalendar(event);
            },
            eventClick: function(event) {
                viewCalendar(event);
            },
            loading: function(bool) {
                if (bool) {
                    $('#loading').show();
                } else {
                    $('#loading').hide();
                }
            },
            editable: true,
            selectable: true,
            selectHelper: true,
            select: function(start, end) {
                openNewCalendarForm(start, end);
                calendar.fullCalendar('unselect');
            }
        });

        $('span.fc-button-prev').before('<span class="fc-button fc-button-add fc-state-default fc-corner-left fc-corner-right">新增</span>');

        $('.fc-button-add').click(function() {
            openNewCalendarForm();
        });

        function openNewCalendarForm(start, end) {
            var url = ctx + '/admin/personal/calendar/new';
            if(start) {
                start = $.fullCalendar.formatDate(start, 'yyyy-MM-dd HH:mm:ss');
                end = $.fullCalendar.formatDate(end, 'yyyy-MM-dd HH:mm:ss');
                url = url + '?start=' + start + '&end=' + end;
            }

            $.app.openDialog('新增提醒事项', url, {btn: false, area: ['500px', '480px'], success: function(){
                $('form').validationEngine({
                    promptPosition : 'bottomRight',
                    autoPositionUpdate:true,
                    scroll:false
                });

                // 日期选择
                $('.date').layDate();

                $('#backgroundColor').change(function() {
                    $(this).attr('style', $(this).find('option:selected').attr('style'));
                });

                $('.all-day').change(function() {
                    if($(this).is(':checked')) {
                        $('[name=startTime],[name=endTime]').val('').attr('disabled', true).removeClass('validate[required]');
                        $('[name=startTime]').validationEngine('hide');
                        $('[name=endTime]').validationEngine('hide');
                    } else {
                        $('[name=startTime],[name=endTime]').removeAttr('disabled').addClass('validate[required]');
                    }
                });
            }});
        }

        function moveCalendar(event) {
            var url = ctx + '/admin/personal/calendar/move';
            var id = event.id;
            var start = $.fullCalendar.formatDate(event.start, 'yyyy-MM-dd HH:mm:ss');
            var end = $.fullCalendar.formatDate(event.end, 'yyyy-MM-dd HH:mm:ss');
            url = url + '?id=' + id;
            url = url + '&start=' + start + '&end=' + end;

            $.post(url, function() {
                calendar.fullCalendar('refetchEvents');
            });
        }

        function viewCalendar(event) {
            var url = ctx + '/admin/personal/calendar/view/' + event.id;
            $.app.openDialog('查看提醒事项', url, {btn: false, area: ['350px', '280px']});
        }

        $(document).on('click', '#btnDelete', function() {
            var $this = $(this);
            $.app.confirm('确认删除提醒事项吗？', function(index){
                var url = ctx + '/admin/personal/calendar/delete?id=' + $this.data('id');
                $.post(url, function() {
                    calendar.fullCalendar('refetchEvents');
                    $.app.close(index);
                });
                return false;
            });
        }).on('click', '#btnClose', function() {
            layer.closeAll();
        }).on('click', '#btnSave', function() {
            var form = $('#editForm');
            if(!form.validationEngine('validate')) {
                return false;
            }
            var url = ctx + '/admin/personal/calendar/new';
            $.post(url, form.serialize(), function() {
                calendar.fullCalendar('refetchEvents');
                $.app.closeDialog();
            });
        });
    }
};